package com.jetlion.sqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.jetlion.sqlite.db.DBManager;
import com.jetlion.sqlite.entity.Student;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private DBManager dbManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        dbManager = new DBManager(this);

        Button addButton = findViewById(R.id.add_btn);
        Button delButton = findViewById(R.id.del_btn);
        Button updateButton = findViewById(R.id.update_btn);
        Button queryButton = findViewById(R.id.query_btn);

        addButton.setOnClickListener(this);
        delButton.setOnClickListener(this);
        updateButton.setOnClickListener(this);
        queryButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.add_btn:
                add();
                break;
            case R.id.del_btn:
                del();
                break;
            case R.id.update_btn:
                update();
                break;
            case R.id.query_btn:
                query();
                break;
            default:
                break;
        }
    }

    public void add() {

        Student luocheng = new Student("智能19101", "罗丞", "男", 100);
        Student zhongwengui = new Student("智能19102", "钟文贵", "男", 100);
        Student chenxinyi = new Student("智能19103", "陈心怡", "女", 100);
        Student tangxuhui = new Student("智能19104", "唐旭辉", "男", 60);

        List<Student> students = new ArrayList<>();
        students.add(luocheng);
        students.add(zhongwengui);
        students.add(chenxinyi);
        students.add(tangxuhui);

        dbManager.add(students);
    }

    public void del() {
        dbManager.delete("智能19101");
    }

    public void update() {
        dbManager.update("智能19104", 90);
    }

    public void query() {
        List<Student> students=dbManager.query();
        Log.i("MainActivity",students.toString());
    }

    @Override
    protected void onDestroy() {
        dbManager.finish();
        super.onDestroy();
    }
}